/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modelo;

import VO.AgendaVO;


import utiles.exceptions.DataBaseException;
import utiles.exceptions.InternalErrorException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Vector;
import javax.sql.DataSource;
import utiles.SQL.GeneralOperations;
import utiles.SQL.SimpleDataSource;

/**
 *
 * @author Jorge
 */
public class DaoAgenda implements DaoAgendaInterface{

    public boolean ingresarContacto(AgendaVO nuevo) throws DataBaseException, InternalErrorException, SQLException {
     Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        boolean result = false;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL ingresarContacto(?,?)";

                callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

                callabledStatement = connection.prepareCall(queryString1);
                callabledStatement.setString("nombre", nuevo.getNombre());
                callabledStatement.setString("fono", nuevo.getFono());


                callabledStatement.execute();
            if (callabledStatement.getUpdateCount() > 0) {
                result = true;
            }

        } catch (SQLException e) {
            //result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

    public boolean editarContacto(AgendaVO nuevo) throws DataBaseException, InternalErrorException, SQLException {
         Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        boolean result = false;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL editarContacto(?,?)";

                callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

                callabledStatement = connection.prepareCall(queryString1);
                callabledStatement.setString("nombre", nuevo.getNombre());
                callabledStatement.setString("fono", nuevo.getFono());


                callabledStatement.execute();
            if (callabledStatement.getUpdateCount() > 0) {
                result = true;
            }

        } catch (SQLException e) {
            //result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

    public boolean eliminarContacto(String nombre) throws DataBaseException, InternalErrorException, SQLException {
         Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        boolean result = false;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL eliminarContacto(?)";

                callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

                callabledStatement = connection.prepareCall(queryString1);
                callabledStatement.setString("nombre", nombre);



                callabledStatement.execute();
            if (callabledStatement.getUpdateCount() > 0) {
                result = true;
            }

        } catch (SQLException e) {
            //result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

    public Vector mostrarContactos() throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        Vector result = null;
        AgendaVO aux = null;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL cargarDatosContactos()";

                callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

                callabledStatement = connection.prepareCall(queryString1);



                callabledStatement.execute();
            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst()!=false) {
                result = new Vector();


                while(resultSet.next())
                {
                    aux = new AgendaVO();
                    aux.setFono(resultSet.getString("telefono"));
                    aux.setNombre(resultSet.getString("nombreInstitucion"));
                    result.add(aux);
                }
            }

        } catch (SQLException e) {
            result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }

        
    }

    public Vector cargarContactos() throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        Vector result = null;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL cargarContactos()";

                callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

                callabledStatement = connection.prepareCall(queryString1);
                


                callabledStatement.execute();
            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst()!=false) {
                result = new Vector();

                while(resultSet.next())
                {
                    result.add(resultSet.getString("id"));
                }
            }

        } catch (SQLException e) {
            result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }

    }

    public AgendaVO cargarContacto(String id) throws DataBaseException, InternalErrorException, SQLException {
            Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        AgendaVO result = null;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL cargarContacto(?)";

                callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

                callabledStatement = connection.prepareCall(queryString1);
                callabledStatement.setString("id",id);




                callabledStatement.execute();
            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst()!=false) {
                result = new AgendaVO();

                while(resultSet.next())
                {
                    result.setNombre(resultSet.getString("nombreInstitucion"));
                    result.setFono(resultSet.getString("telefono"));
                }
            }

        } catch (SQLException e) {
            result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

}
